package base

import (
	"context"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/dto/base"
	orgRepo "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/base/org"
	basePb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	orgService "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/base/org"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
	"time"
)

type baseOrgServer struct {
	orgService orgService.OrgServiceIface
}

func NewBaseOrgServer(orgService orgService.OrgServiceIface) basePb.BaseOrgServiceServer {
	return &baseOrgServer{
		orgService: orgService,
	}
}

// OrgSave 创建或更新
func (s *baseOrgServer) OrgSave(ctx context.Context, req *basePb.OrgSaveRequest) (*basePb.OrgSaveResponse, error) {
	var response = &basePb.OrgSaveResponse{}
	if req.Param == nil {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "参数为空")
	}

	if req.Param.OrgName == "" {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "部门名称为空")
	}

	//if req.Param.ParentId == 0 {
	//	return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "父级部门编号为空")
	//}

	orgObj := &orgRepo.GfyOrganizationPo{
		Id:         req.GetParam().GetId(),
		Name:       req.GetParam().GetOrgName(),
		ParentId:   req.GetParam().ParentId,
		Sort:       0,
		CreatedOn:  time.Now(),
		CreatedBy:  "",
		ModifiedOn: time.Now(),
		ModifiedBy: "",
	}

	orgId, err := s.orgService.Save(ctx, orgObj, req.GetParam().AreaCodes)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgSave-error: %+v, orgObj: %+v", err, orgObj)
		return response, errcode.CTogRPCError(err)
	}

	response.Id = orgId

	return response, nil
}

// OrgDelete 删除组织
func (s *baseOrgServer) OrgDelete(ctx context.Context, req *basePb.OrgDeleteRequest) (*basePb.OrgDeleteResponse, error) {
	response := &basePb.OrgDeleteResponse{}
	if req.OrgId == 0 {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "部门编号为空")
	}

	err := s.orgService.Delete(ctx, req.OrgId)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgDelete-error: %+v, org_id: %d", err, req.OrgId)
		return response, errcode.CTogRPCError(err)
	}

	response.Id = req.OrgId
	return response, nil
}

// OrgList 组织列表
func (s *baseOrgServer) OrgList(ctx context.Context, req *basePb.OrgListRequest) (*basePb.OrgListResponse, error) {
	response := &basePb.OrgListResponse{}
	response.List = make([]*basePb.OrgItem, 0)
	list, err := s.orgService.List(ctx)
	if err != nil {
		stark.Logger.Infof(ctx, "OrgList-error: %+v", err)
		return response, errcode.CTogRPCError(err)
	}

	for _, item := range list {
		pbItem := &basePb.OrgItem{}
		userResponsible := make([]*basePb.UserResponsible, 0)
		for _, item := range item.UserResponsible {
			userResponsible = append(userResponsible, &basePb.UserResponsible{
				OrgId:  item.OrganizationId,
				UserId: item.UserId,
				Name:   item.Name,
			})
		}
		if item.OrgItem != nil {
			pbItem.ParentId = item.OrgItem.ParentId
			pbItem.OrgName = item.OrgItem.Name
			pbItem.Id = item.OrgItem.Id
			pbItem.FullName = item.OrgItem.FullName
			pbItem.AreaCodes = item.AreaCodes
			pbItem.UserResponsible = userResponsible
		}

		//if item.AreaList != nil {
		//	pbItem.AreaList = make([]*basePb.AreaItem, 0)
		//	for _, area := range item.AreaList {
		//		// TODO
		//		println("area: ", area)
		//		pbItem.AreaList = append(pbItem.AreaList, &basePb.AreaItem{
		//			Id:           0,
		//			DisplayOrder: "",
		//			ItemCode:     "",
		//			ItemName:     "",
		//			NormName:     "",
		//			ParentCode:   "",
		//		})
		//	}
		//}
		response.List = append(response.List, pbItem)
	}

	return response, nil
}

// ImportOrgs 组织列表
func (s *baseOrgServer) ImportOrgs(ctx context.Context, req *basePb.ImportOrgRequest) (*basePb.ImportOrgResponse, error) {
	response := &basePb.ImportOrgResponse{}
	if (req.OrgList == nil) || len(req.OrgList) == 0 {
		return response, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "导入组织为空")
	}

	orgList := []*base.ImportOrgDto{}
	for _, org := range req.OrgList {
		orgItem := &base.ImportOrgDto{
			OrgFullName: org.OrgFullName,
			AreaNames:   org.AreaNames,
		}
		orgList = append(orgList, orgItem)
	}

	resp, err := s.orgService.ImportOrgs(ctx, orgList)
	if err != nil {
		stark.Logger.Errorf(ctx, "ImportOrgs-err: %+v, req: %+v", err, req)
		return resp, errcode.CTogRPCError(err)
	}

	return resp, nil
}
